﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;

namespace YArchitech.HVAC
{
	public partial class FormSelCreate : System.Windows.Forms.Form
	{
		private void FrmParentMethod()
		{
			if (this.classSel != null)
			{
				this.classSel();
			}
		}

		public FormSelCreate(ExternalCommandData cmdData, string msg, ElementSet elements, ICollection<Element> collection, string strCurrApp)
		{
			this.InitializeComponent();
			this.cmdDataForm = cmdData;
			this.msgForm = msg;
			this.elementsForm = elements;
			this.frmCollection = collection;
			this.currentApp = strCurrApp;
			base.ShowInTaskbar = false;
			this.BindPro();
		}

		private void BindPro()
		{
			if (this.cmdDataForm.Application.ActiveUIDocument == null)
			{
				return;
			}
			UIDocument activeUIDocument = this.cmdDataForm.Application.ActiveUIDocument;
			if (activeUIDocument.Document.Title != "")
			{
				this.proTitle = activeUIDocument.Document.Title;
			}
			if (activeUIDocument.Document.PathName != "")
			{
				this.proPath = activeUIDocument.Document.PathName;
			}
			XmlInfo.RegForm(this.proTitle, this.proPath);
		}

		public FormSelCreate()
		{
			this.InitializeComponent();
			base.ShowInTaskbar = false;
		}

		private void SetPos()
		{
			System.Drawing.Point point = base.PointToScreen(this.panel1.Location);
			this.frmLevel.Owner = this;
			this.frmLevel.Top = point.Y;
			this.frmLevel.Left = point.X;
			point = base.PointToScreen(this.panel2.Location);
			this.frmCate.Owner = this;
			this.frmCate.Top = point.Y;
			this.frmCate.Left = point.X;
		}

		private void btnLevel_Click(object sender, EventArgs e)
		{
			try
			{
				this.frmLevel.Close();
				this.frmLevel = new FrmListLevel(this.cmdDataForm, this.currentApp);
				this.frmLevel.Owner = this;
				FrmListLevel frmListLevel = this.frmLevel;
				frmListLevel.classSel = (FrmListLevel.delegateSel)Delegate.Combine(frmListLevel.classSel, new FrmListLevel.delegateSel(this.LevelFirstChange));
				this.frmLevel.Show();
				this.SetPos();
			}
			catch (Exception ex)
			{
				ex.ToString();
				MessageBox.Show("操作失败。");
			}
		}

		private void btnLevel_Enter(object sender, EventArgs e)
		{
			this.txtLevel.Focus();
		}

		private void btnCate_Click(object sender, EventArgs e)
		{
			try
			{
				this.frmCate.Close();
				this.frmCate = new FrmListCate(this.currentApp, this.cmdDataForm);
				this.frmCate.Owner = this;
				FrmListCate frmListCate = this.frmCate;
				frmListCate.classSel = (FrmListCate.delegateSel)Delegate.Combine(frmListCate.classSel, new FrmListCate.delegateSel(this.CateFirstChange));
				this.frmCate.Show();
				this.SetPos();
			}
			catch (Exception ex)
			{
				ex.ToString();
				MessageBox.Show("操作失败。");
			}
		}

		private void btnCate_Enter(object sender, EventArgs e)
		{
			this.txtLevel.Focus();
		}

		private void panel1_Paint(object sender, PaintEventArgs e)
		{
			WinFormTools.SetPanelColor(this.panel1, e);
		}

		private void panel2_Paint(object sender, PaintEventArgs e)
		{
			WinFormTools.SetPanelColor(this.panel2, e);
		}

		private void FormSelCreate_Move(object sender, EventArgs e)
		{
			this.SetPos();
		}

		private void FormSelCreate_Load(object sender, EventArgs e)
		{
			this.timer1.Start();
		}

		private void txtLevel_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = true;
		}

		private void txtCate_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = true;
		}

		private bool CreateSelectStr()
		{
			if (this.txtName.Text == "")
			{
				MessageBox.Show("请输入选择集名称！");
				return false;
			}
			XmlDocument xmlDocument = new XmlDocument();
			string pathName = this.cmdDataForm.Application.ActiveUIDocument.Document.PathName;
			string title = this.cmdDataForm.Application.ActiveUIDocument.Document.Title;
			xmlDocument.LoadXml(XmlInfo.GetSelSet(pathName, title));
			if (xmlDocument.SelectSingleNode("Elements/Select[@Name='" + this.txtName.Text + "']") != null)
			{
				MessageBox.Show("选择集名称已经存在！");
				return false;
			}
			UIDocument activeUIDocument = this.cmdDataForm.Application.ActiveUIDocument;
            Autodesk.Revit.ApplicationServices.Application application = this.cmdDataForm.Application.Application;
			return CmdTools.FormCreateColl(this.cmdDataForm, this.txtName.Text) == null;
		}

		private void CreateThread()
		{
			CmdTools.FilterSel(this.cmdDataForm);
            System.Windows.Forms.Form parentForm = base.ParentForm;
			if (this.CreateSelectStr())
			{
				this.FrmParentMethod();
				MessageBox.Show("选择集创建成功！");
			}
		}

		private void btnCreate_Click(object sender, EventArgs e)
		{
			try
			{
				base.Enabled = false;
				this.CreateThread();
				base.Activate();
				base.Enabled = true;
			}
            catch (Autodesk.Revit.Exceptions.InvalidOperationException ex)
			{
				ex.ToString();
				MessageBox.Show("上一个操作未完成。");
				base.Activate();
				base.Enabled = true;
			}
			catch (Exception ex2)
			{
				MessageBox.Show(ex2.ToString());
				base.Activate();
				base.Enabled = true;
			}
		}

		private void LevelFirstChange()
		{
			this.chkLevel.Checked = XmlInfo.LevelFirst();
		}

		private void CateFirstChange()
		{
			this.chkCate.Checked = XmlInfo.CateFirst();
		}

		private void btnFilter_Click(object sender, EventArgs e)
		{
			try
			{
				base.Enabled = false;
				CmdTools.FilterSel(this.cmdDataForm);
				base.Enabled = true;
			}
			catch (Exception ex)
			{
				ex.ToString();
				MessageBox.Show("操作失败！");
				base.Enabled = true;
			}
		}

		private void chkLevel_CheckedChanged(object sender, EventArgs e)
		{
			XmlInfo.ChangeFirstLevel(this.chkLevel.Checked);
		}

		private void chkCate_CheckedChanged(object sender, EventArgs e)
		{
			XmlInfo.ChangeFirstCate(this.chkCate.Checked);
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			UIDocument activeUIDocument = this.cmdDataForm.Application.ActiveUIDocument;
			if (System.Windows.Forms.Form.ActiveForm != null)
			{
				if (this.proTitle != activeUIDocument.Document.Title || this.proPath != activeUIDocument.Document.PathName)
				{
					base.Hide();
				}
				return;
			}
			if (WinFormTools.GetForegroundWindow().ToString() != this.currentApp)
			{
				base.Hide();
				return;
			}
			if (this.cmdDataForm.Application.ActiveUIDocument == null)
			{
				base.Hide();
				return;
			}
			if (activeUIDocument.Document.Title != this.proTitle)
			{
				base.Hide();
				return;
			}
			if (activeUIDocument.Document.PathName != this.proPath)
			{
				base.Hide();
			}
		}

		private ExternalCommandData cmdDataForm;

		private string msgForm;

		private ElementSet elementsForm = new ElementSet();

		private ICollection<Element> frmCollection;

		public FormSelCreate.delegateSel classSel;

		private string proTitle = "";

		private string proPath = "";

		private string currentApp = "";

		private FrmListLevel frmLevel = new FrmListLevel();

		private FrmListCate frmCate = new FrmListCate();

		public delegate void delegateSel();
	}
}
